package com.easyschool.webservices;

import java.sql.Date;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import com.easyschool.model.bo.BoClass;
import com.easyschool.model.bo.BoSchool;
import com.easyschool.model.school.Class;
import com.easyschool.model.school.ConfirmClass;
import com.easyschool.model.school.ConfirmSchool;
import com.easyschool.model.school.School;

@Path("/school")
public class SchoolWS {

	@Path("/getAllSchools")
	@GET
	@Produces({"application/xml", "application/json"})
	public List<School> getAllSchools() throws Exception{
		List<School> list = BoSchool.getInstance().getAllSchools();
		return list;
	}
	
	@Path("/getAllSchoolsByFilters")
	@GET
	@Produces({"application/xml", "application/json"})
	public List<School> getAllSchoolsByFilters(@QueryParam("libelle") String libelle, @QueryParam("adresse1") String adresse1, @QueryParam("adresse2") String adresse2, @QueryParam("adresse3") String adresse3, @QueryParam("codePostal") Integer codePostal, @QueryParam("ville") String ville) throws Exception{
		List<School> list = BoSchool.getInstance().getAllSchoolsByFilters(libelle, adresse1, adresse2, adresse3, codePostal, ville);
		return list;
	}
	
	@Path("/addSchool")
	@POST
	@Produces({"application/xml", "application/json"})
    @Consumes("application/x-www-form-urlencoded")
	public School addSchool(@FormParam("libelle") String libelle, @FormParam("adresse1") String adresse1, @FormParam("adresse2") String adresse2, @FormParam("adresse3") String adresse3, @FormParam("codePostal") String codePostal, @FormParam("ville") String ville) throws Exception{
		
		School school = new School(null, libelle, adresse1, adresse2, adresse3, new Integer(codePostal), ville, new Date(new java.util.Date().getTime()));
		school.setIdEcole(BoSchool.getInstance().addSchool(school));
		return school;
	}
	
	@Path("/removeSchool")
	@GET
	@Produces({"application/xml", "application/json"})
	public ConfirmSchool removeSchool(@QueryParam("id") Integer id) throws Exception{
		ConfirmSchool confirm = new ConfirmSchool();
		confirm.setId(id);
		BoSchool.getInstance().removeUser(id);
		return confirm;
	}
	
	@Path("/getAllClasses")
	@GET
	@Produces({"application/xml", "application/json"})
	public List<com.easyschool.model.school.Class> getAllClasses() throws Exception{
		List<com.easyschool.model.school.Class> list = BoClass.getInstance().getAllClasses();
		return list;
	}
	
	@Path("/getAllClassesByFilters")
	@GET
	@Produces({"application/xml", "application/json"})
	public List<Class> getAllClassesByFilters(@QueryParam("libelle") String libelle, @QueryParam("schoolId") Integer schoolId) throws Exception{
		List<Class> list = BoClass.getInstance().getAllClassesByFilters(libelle, schoolId);
		return list;
	}
	
	@Path("/addClass")
	@POST
	@Produces({"application/xml", "application/json"})
    @Consumes("application/x-www-form-urlencoded")
	public Class addClass(@FormParam("libelle") String libelle, @FormParam("schoolId") String schoolId) throws Exception{
		
		Class theClass = new Class(null, libelle, new Date(new java.util.Date().getTime()), new Integer(schoolId));
		theClass.setIdClass(BoClass.getInstance().addClass(theClass));
		return theClass;
	}
	
	@Path("/removeClass")
	@GET
	@Produces({"application/xml", "application/json"})
	public ConfirmClass removeClass(@QueryParam("id") Integer id) throws Exception{
		ConfirmClass confirm = new ConfirmClass();
		confirm.setId(id);
		BoClass.getInstance().removeClass(id);
		return confirm;
	}
	
	@Path("/getAllSchoolClasses")
	@GET
	@Produces({"application/xml", "application/json"})
	public List<Class> getAllSchoolClasses(@QueryParam("schoolId") String schoolId) throws Exception{
		return BoClass.getInstance().getClassesBySchool(new Integer(schoolId));
	}
	
	
	@Path("/getSchoolClassById")
	@GET
	@Produces({"application/xml", "application/json"})
	public Class getSchoolClassById(@QueryParam("idClass") String idClass) throws Exception{
		return BoClass.getInstance().getClassById(new Integer(idClass));
	}
	

	
	@Path("/getSchoolById")
	@GET
	@Produces({"application/xml", "application/json"})
	public School getSchoolById(@QueryParam("idSchool") String idSchool) throws Exception{
		return BoSchool.getInstance().getSchool(new Integer(idSchool));
	}
}
